package com.example.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;

import static com.example.ulit.EmailUtil.sendString;

@Component
public class Consumer {
    @RabbitListener(queues = "queue")
    public void recipient(byte[] messageBytes){
        try {
            // 将字节数组转换为输入流
            ByteArrayInputStream bis = new ByteArrayInputStream(messageBytes);
            ObjectInputStream ois = new ObjectInputStream(bis);

            // 从输入流中读取对象
            Map<String, Object> message = (Map<String, Object>) ois.readObject();

            // 关闭输入流
            ois.close();
            bis.close();

            // 现在可以处理消息了
            sendString(message.get("email").toString(), "注册成功", "您的注册账号为:" + message.get("account") + "!");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
